/**
  * Copyright (c) CSIRO Australia, 2009
  *
  * @author $Author: jia020 $
  * @version $Id: NavigationPanel.java 643 2009-05-20 00:20:55Z jia020 $
  */
package au.csiro.nt.pdsp.client;

import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.AccordionLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
/**
 * NavigationPanel on the left-hand in the viewpoint.
 * Based on the user's role to create operation panel. 
 */
public class NavigationPanel extends Panel {
	/**
	 * role of user. 
	 */	
	private String role = null;
	/**
	 * eventHandler for callback. 
	 */		
	private EventHandler eventHandler = null;
	/**
	 * service point for rpc call. 
	 */		
	private final PDQueryServiceAsync service = GWT.create(PDQueryService.class);		
	/**
	 * Constructor for NavigationPanel. 
	 * @param eventHandler eventHandler for callback.
	 */		
	public NavigationPanel(final EventHandler eventHandler) {
		this.eventHandler = eventHandler;
		this.role = GlobalVars.getInstance().role;
		this.setLayout(new AccordionLayout(true));
		
//		Panel panelOne = new Panel("Registration");
//		panelOne.setIconCls("settings-icon");
//		Label lblRegisterAs = new Label("Register As:");
//		panelOne.add(lblRegisterAs);
//		Hyperlink hyperlink_1 = new Hyperlink("Individual", false,
//				"newHistoryToken");
//		panelOne.add(hyperlink_1);
//		Hyperlink hyperlink_2 = new Hyperlink("Organization", false,
//				"newHistoryToken");
//		panelOne.add(hyperlink_2);
//		Hyperlink hyperlink = new Hyperlink("Administrator", false,
//				"newHistoryToken");
//		panelOne.add(hyperlink);
//		this.add(panelOne);
		Panel panelOne = new Panel("Registration");
		panelOne.setIconCls("settings-icon");
		Panel panelTwo = new Panel("Operation");
		panelTwo.setIconCls("folder-icon");
		Panel panelThree = new Panel("Administration");
		panelThree.setIconCls("user-add-icon");
		if (role.equalsIgnoreCase(PdspConst.ROLE_INDIVIDUAL)) {
			PushButton pbMessages = new PushButton("Get messages");
			pbMessages.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					service.getMyMessages(GlobalVars.getInstance().userid, new AsyncCallback<String>() {
						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
						  MessageBox.alert("failed:getMyMessages");
						}
	
						public void onSuccess(String result) {
							//MessageBox.alert(result);	
							eventHandler.processEvent(PdspConst.EVENT_SHOW_MESSAGES,result);
						}
					});						
				}
			});			
			panelTwo.add(pbMessages);	
			String status = GlobalVars.getInstance().status;
			final String label = (status.equals(PdspConst.USER_STATUS_SUBSCRIBED))? "Unsubscribe":"Subscribe";
			PushButton pbSubscribe = new PushButton(label);
			pbSubscribe.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
							eventHandler.processEvent(PdspConst.EVENT_SHOW_SUBSCRIBE,label);					
				}
			});		
			panelTwo.add(pbSubscribe);				
		}		
		if (role.equalsIgnoreCase(PdspConst.ROLE_ORGANISATION)) {
			PushButton pbQuery = new PushButton("Query");
			pbQuery.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					eventHandler.processEvent(PdspConst.EVENT_SHOW_QUERY,"");
				}
			});			
			panelTwo.add(pbQuery);
			PushButton pbMyTask = new PushButton("MyTasks");
			pbMyTask.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					service.getMyTasks(GlobalVars.getInstance().userid, new AsyncCallback<String>() {
						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
						  MessageBox.alert("failed:getMyTasks");
						}
	
						public void onSuccess(String result) {
							//MessageBox.alert(result);	
							eventHandler.processEvent(PdspConst.EVENT_SHOW_MYTASKS,result);
						}
					});							
	
				}
			});
			panelTwo.add(pbMyTask);
		}
		if (role.equalsIgnoreCase(PdspConst.ROLE_ADMIN)) {
			PushButton pbReview = new PushButton("Review workflows");
			panelThree.add(pbReview);
			pbReview.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
			  	service.getUnreviewedWorkflows(new AsyncCallback<String>(){
						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
						  MessageBox.alert("failed:getUnreviewedWorkflows!");
						}
						@Override
						public void onSuccess(String result) {
							eventHandler.processEvent(PdspConst.EVENT_SHOW_UNREVIEWEDWORKFLOWS,result);
						}		  
			  	});						
				}
			});		
			PushButton pbAdminWorkflows = new PushButton("Admin workflows");
			panelThree.add(pbAdminWorkflows);
			pbAdminWorkflows.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
			  	service.getWorkflows(PdspConst.WF_GET_ALL,0,new AsyncCallback<String>(){
						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
						  MessageBox.alert("failed:getWorkflows!");
						}
						@Override
						public void onSuccess(String result) {
							eventHandler.processEvent(PdspConst.EVENT_SHOW_WORKFLOWS,result);
						}		  
			  	});						
				}
			});				
			PushButton pbAdminPolicy = new PushButton("Admin Policy");
			panelThree.add(pbAdminPolicy);
			PushButton pbAdminUsers = new PushButton("Admin Users");
			panelThree.add(pbAdminUsers);
			PushButton pbAdminTool = new PushButton("Admin Tools");
			panelThree.add(pbAdminTool);
		}
		
		this.add(panelOne);			
		this.add(panelTwo);		
		this.add(panelThree);

		this.setTitle("Navigation");
		this.setHeight(400);
		this.setWidth(200);
	}
}
